home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / qbfaqr01.zip / WC.BAS < prev    next >
BASIC Source File  |  1992-08-10  |  2KB  |  69 lines

  1. 'Date: 05-11-92 (22:51)
  2. 'From: BRENT ASHLEY
  3. '---------------------------------------------------------------------------
  4. 'Speaking of contests...
  5.  
  6. 'I got a call today from none other than Ethan Winer, saying I've won
  7. 'Crescent's "word count" programming contest!  The idea was to write
  8. 'the fastest all-qb program to count the words in a text file.  This one
  9. 'does it on a 350k file in 1.4 seconds on my SX.  Ethan said I could post
  10. 'it about, so here goes:
  11.  
  12. ' Compile:  BC /o/a wc,,wc;
  13. ' Link:     LINK /ex/noe wc+nocom;
  14. '
  15. DEFINT A-Z
  16. DIM FBuf AS STRING * 8192              ' Use fixed string to fix
  17.                                        ' position
  18.  
  19. WCount% = -32768                       ' Init count to bottom of integer
  20.                                        ' range
  21.                                        ' to get 65535 counts available
  22.  
  23. DEF SEG = VARSEG(FBuf)                 ' Point to fixed buffer in memory
  24. BufStart% = VARPTR(FBuf)
  25. BufLen% = 8192
  26.  
  27. NotOnWord = -1                         ' Assume not on word to start
  28.  
  29. OPEN COMMAND$ FOR BINARY AS #1         ' Open file and store length
  30. AmtLeft& = LOF(1)
  31.  
  32. DO                                     ' Process file
  33.  
  34.   IF AmtLeft& >= BufLen% THEN GOTO NotLastBlock
  35.  
  36.   BufLen% = AmtLeft&                   ' Last block - size accordingly
  37.   Done% = -1                           '  and flag end of loop
  38.   GOTO GetBuf                          ' Less likely event gets GOTO
  39.  
  40. NotLastBlock:                          ' More likely event falls thru to
  41.                                        ' GetBuf
  42.   AmtLeft& = -BufLen% + AmtLeft&       ' Negative first saves bytes
  43.  
  44. GetBuf:
  45.   GET #1, , FBuf                       ' Fill buffer
  46.  
  47.   FOR Ofs% = BufStart% TO BufStart% + BufLen% - 1  ' Traverse buffer
  48.  
  49.     IF PEEK(Ofs%) > 32 THEN GOTO NotWhiteSpace
  50.  
  51.     IF NotOnWord% THEN GOTO NextByte   ' If already white space...
  52.  
  53.     NotOnWord% = -1                    ' Trailing edge of word
  54.     WCount% = WCount% + 1              '  triggers counter
  55.     GOTO NextByte
  56.  
  57. NotWhiteSpace:                         ' Not white space is more likely
  58.     NotOnWord% = 0                     '  so comparison done on white
  59.                                        '  space
  60.  
  61. NextByte:
  62.   NEXT
  63. LOOP UNTIL Done%                       ' post-compare saves bytes
  64.  
  65. PRINT WCount% + 32768                  ' display word count
  66.  
  67.  
  68. '---- end of program ----
  69.